package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.PatientAssessBean;
import com.hxkcloud.patient.utils.Utils;
import com.hxkcloud.patient.views.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-08-06.
 * Description:患者，消息，评估提醒列表
 */
public class MonitorInfoPAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<PatientAssessBean> datas;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader;
    private onItemSelectListener listener;

    public MonitorInfoPAdapter(Context mContext, List<PatientAssessBean> datas,
                               onItemSelectListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    public void setData(List<PatientAssessBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;
        PatientAssessBean bean = datas.get(position);
        mHolder.iv_head.setVisibility(View.GONE);
        mHolder.tv_name.setVisibility(View.GONE);
        mHolder.tv_title.setText(bean.getNurseName());
//        mHolder.tv_date.setText(Utils.timeStamp2Date(datas.get(position).getTime(), "yyyy-MM-dd"));
        String dateTime= Utils.timeStamp2Date(bean.getTime(),"yyyy-MM-dd HH:mm:ss");

            String date1 = dateTime.split(" ")[0];
            Date dt=new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String nowTime = df.format(dt);
            long days = getDaySub(date1,nowTime);
            if(days == 0){
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.today));
            }else if(days == 1){
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.yesterday));
            }else{
                mHolder.tv_date.setText(date1);
            }



        mHolder.tv_intro.setText("为了更好地了解您的身体情况,需要您配合完成评估:" + bean.getIntro() + ",请及时作答。");
        mHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onItemSelect(position);
            }

        });


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_monitorinfo, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    private long getDaySub(String beginDateStr,String endDateStr)
    {
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate;
        java.util.Date endDate;
        try
        {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        return day;
    }

    public class Holder extends RecyclerView.ViewHolder {

        private LinearLayout layout;
        private CircleImageView iv_head;
        private TextView tv_title;
        private TextView tv_name;
        private TextView tv_date;
        private TextView tv_intro;

        public Holder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.tv_monitorinfo_info);
            iv_head = (CircleImageView) view.findViewById(R.id.iv_monitorinfo_head);
            tv_title = (TextView) view.findViewById(R.id.tv_monitorinfo_title);
            tv_name = (TextView) view.findViewById(R.id.tv_monitorinfo_name);
            tv_date = (TextView) view.findViewById(R.id.tv_monitorinfo_date);
            tv_intro = (TextView) view.findViewById(R.id.tv_monitorinfo_intro);
        }
    }

    public interface onItemSelectListener {
        void onItemSelect(int position);
    }
}
